package io.renren.modules.noc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.noc.entity.NocReceiptEntity;
import io.renren.modules.noc.service.NocResultService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 统计报表
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-05-05 22:51:12
 */
@RestController
@RequestMapping("noc/nocresult")
public class NocResultController extends BaseController{
    @Autowired
    private NocResultService nocResultService;

    /**
     * 列表
     */
    @RequestMapping("/getResult")
    public R list(HttpServletRequest req){
        Integer tid = this.getDefaultTemplateId();
        if(tid==null){
            return R.error();
        }
        String start = req.getParameter("startDate");
        String end = req.getParameter("endDate");
        System.out.println(start);
        List<Map<String,Object>> totalResult = nocResultService.getTotalResult(tid.toString(),start,end);
        List<Map<String,Object>> newResult = nocResultService.getNewResult(tid.toString(),start,end);
        //List<Map<String,Object>> recomeResult = nocResultService.getRecomeResult(tid.toString(),start,end);
        Map<String,Object> result = new HashMap<>();
        result.put("totalResult",totalResult);
        result.put("newResult",newResult);
        //result.put("recomeResult",recomeResult);
        req.setAttribute("startDate",start);
        req.setAttribute("endDate",end);
        return R.ok().put("result", result);
    }

    //观众回执表
    @RequestMapping("/getMemberReceipt")
    public R getMemberReceipt(@RequestParam Map<String, Object> params){
        String current = (String)params.get("page");
        String limit = (String)params.get("limit");
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(Integer.parseInt(current), Integer.parseInt(limit));
        List<Map<String,Object>> result = nocResultService.getMemberReceipt(page,params);
        //添加合计开始
        /*
        Map<String,Object> totalCount = new HashMap<>();
        totalCount.put("ly","总计");
        long personCount = 0;
        for(Map<String,Object> map:result){
            personCount += (long)map.get("personCount");
        }
        Map<String,Object> map = nocResultService.getCompanyReceipt(params);
        totalCount.put("personCount",personCount);
        totalCount.put("companyCount",companyCount);
        result.add(totalCount);
        */
        //添加合计结束
        String total = page.getTotal()+"";
        String size = page.getSize()+"";
        String curr = page.getCurrent()+"";
        PageUtils page2 = new PageUtils(result,Integer.parseInt(total),Integer.parseInt(size),Integer.parseInt(curr));
        return R.ok().put("page", page2);
    }

    @RequestMapping("/getMemberReceiptCount")
    public R getMemberReceiptCount(@RequestParam Map<String, Object> params){
        Map<String,Object> map = nocResultService.getCompanyReceipt(params);
        return R.ok().put("map", map);
    }
    //各来源报名及到场情况表
    @RequestMapping("/getMemberPresent")
    public R getMemberPresent(@RequestParam Map<String, Object> params){
        String current = (String)params.get("page");
        String limit = (String)params.get("limit");
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(Integer.parseInt(current), Integer.parseInt(limit));
        List<Map<String,Object>> result = nocResultService.getMemberPresent(page,params);
        //添加合计开始
        /*
        Map<String,Object> totalCount = new HashMap<>();
        totalCount.put("ly","总计");
        long personCount = 0;
        for(Map<String,Object> map:result){
            personCount += (long)map.get("personCount");
        }
        Map<String,Object> map = nocResultService.getCompanyReceipt(params);
        totalCount.put("personCount",personCount);
        totalCount.put("companyCount",companyCount);
        result.add(totalCount);
        */
        //添加合计结束
        String total = page.getTotal()+"";
        String size = page.getSize()+"";
        String curr = page.getCurrent()+"";
        PageUtils page2 = new PageUtils(result,Integer.parseInt(total),Integer.parseInt(size),Integer.parseInt(curr));
        return R.ok().put("page", page2);
    }
    //展会现场观众到场情况统计
    @RequestMapping("/nocAudiencePresent")
        public R nocAudiencePresent(@RequestParam Map<String, Object> params){
        String current = (String)params.get("page");
        String limit = (String)params.get("limit");
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(Integer.parseInt(current), Integer.parseInt(limit));
        List<Map<String,Object>> result = nocResultService.getAudiencePresent(page,params);
        //添加合计开始
        /*
        Map<String,Object> totalCount = new HashMap<>();
        totalCount.put("ly","总计");
        long personCount = 0;
        for(Map<String,Object> map:result){
            personCount += (long)map.get("personCount");
        }
        Map<String,Object> map = nocResultService.getCompanyReceipt(params);
        totalCount.put("personCount",personCount);
        totalCount.put("companyCount",companyCount);
        result.add(totalCount);
        */
        //添加合计结束
        String total = page.getTotal()+"";
        String size = page.getSize()+"";
        String curr = page.getCurrent()+"";
        PageUtils page2 = new PageUtils(result,Integer.parseInt(total),Integer.parseInt(size),Integer.parseInt(curr));
        return R.ok().put("page", page2);
    }


}
